<template>
    <button class="verification" @click='getVerification' :disabled="disableBtn">{{countext}}</button>
</template>
<script>
    import { Toast } from 'vant';
    export default{
        data(){
            return{
                countext: "短信验证码",
                second: 120,
                disableBtn:false
            }
        },
        props:['phone', 'token', 'webUserId', 'count'],
        methods:{
            //获取验证码
            getVerification () {
                var _this = this;
                if(!_this.Tools.regCombination.null.test(_this.phone)){
                    Toast({message: '请输入手机号码', position: 'bottom'})
                }else if(!_this.Tools.regCombination.phone.test(_this.phone)){
                    Toast({message: '请输入正确的手机号码', position: 'bottom'})
                }else{
                    //各种情况下请求验证码接口
                    if(this.$route.name == 'register'){
                        this.gainCode({type:'reg'});   //注册
                    }else if(this.$route.query.type == 1){
                        this.gainCode({type:'findpwd'});   //登录密码
                    }else if(this.$route.query.type == 2){
                        this.gainCode({type:'findpwd_pay'});   //支付密码
                    }else if(this.$route.name == 'reviseMobile'){
                        this.gainCode({type:'edit_moble'});   //更换手机号
                    }
                }
            },
            //请求验证码接口
            gainCode:function(json){
				let _this = this;
                this.disableBtn = true;
				this.Tools.AXIOS({
                    url:'passport/sms',
					transformRequest:{
                        moble: this.phone,
                        type: json.type,
					},
					success: function(res){
                        Toast({message: res.data.msg, position: 'bottom'})
                        _this.codetime();
                    },
                    error(res){
                        _this.disableBtn = false;
                    }
				})
            },
            codetime:function () {
                var _this = this;
                if(this.second === 0){                    
                    this.disableBtn = false;
                    this.countext = "短信验证码";
                    this.second = 120;
                }else{
                    this.disableBtn = true;
                    this.countext = this.second + '秒';
                    this.second --;
                    setTimeout(function () {
                        _this.codetime()
                    },1000)
                }
            }
        },
		components:{
            Toast
		}
    }
</script>